package com.rideshare.distance;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;


public class CalculateDistance {

public CalculateDistance()
{
	
}


public double distance(double lat1, double lon1, double lat2, 
		double lon2, char unit)
	{
	  double theta = lon1 - lon2;
	  double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
	  dist = Math.acos(dist);
	  dist = rad2deg(dist);
	  dist = dist * 60 * 1.1515;
	  if (unit == 'K') {
	    dist = dist * 1.609344;
	  } else if (unit == 'N') {
	  	dist = dist * 0.8684;
	    }
	  return (dist);
	}


private double rad2deg(double dist) {
	return (dist * 180 / Math.PI);
	
}


private double deg2rad(double lat) {
	return (lat * Math.PI / 180.0);
	}

public double distance(String street, String city, String state)
{
	double dis=0;
	//http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,
	//+Mountain+View,+CA&output=json&oe=utf8&sensor=true_or_false&key=your_api_key
	
	String url = String.format("http://maps.google.com/maps/geo?q={0},{1},{2}", street, city, state);
	  
	DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse (new File("book.xml"));
    
    Double latitude= 0.0;
    Double longitude= 0.0;

	

	String geocoderUri = String.Format(
		    "http://rpc.geocoder.us/service/rest?address={0},{1},{2}",
		    street, city, state);
		XmlDocument geocoderXmlDoc = new XmlDocument();
		geocoderXmlDoc.Load(geocoderUri);
	
		XmlNamespaceManager nsMgr =
		    new XmlNamespaceManager(geocoderXmlDoc.NameTable);
		nsMgr.AddNamespace("geo",
		    @"http://www.w3.org/2003/01/geo/wgs84_pos#");

		string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(
		        @"//geo:long", nsMgr).InnerText;
		string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(
		        @"//geo:lat", nsMgr).InnerText;

		double latitude = Double.Parse(sLat);
		double longitude = Double.Parse(sLong);

		Console.WriteLine("Lat: " + latitude + " Lon: " + longitude);
		
	return dis;
}

public double changeZipcodeToLonglat(double d)
{
	double x = 0;
	
	
	
	return x;
}

}
